<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加角色</title>
    <link rel="stylesheet" href="__COMMON__/dtree/dtree.css">
    <link rel="stylesheet" href="__COMMON__/dtree/font/dtreefont.css">
    <link rel="stylesheet" href="__COMMON__/css/simple.css">
    <link rel="stylesheet" href="__STATIC__/layui/css/layui.css">
    <script src="__COMMON__/jquery.min.js"></script>
</head>
<body>

<form class="layui-form" action="" style="padding: 10px">
    <div class="layui-form-item">
        <label class="layui-form-label"><em style="color: red">*</em> 名称</label>
        <div class="layui-input-block">
            <input type="text" name="role_name" required  lay-verify="required" autocomplete="off" class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="启用" checked>
            <input type="radio" name="status" value="0" title="禁用">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-block">
            <div id="tree" class="demo-tree-more"></div>
        </div>
    </div>

    <div class="layui-form-item" style="margin-top: 40px">
        <div class="layui-input-block">
            <button type="button" class="am-btn am-btn-success am-btn-sm" lay-submit lay-filter="subForm">立即提交</button>
            <button type="reset" class="am-btn am-btn-default am-btn-sm" >重置</button>
        </div>
    </div>
</form>

<script src="__STATIC__/layui/layui.js"></script>
<script>

    var data = {$node|raw};

    layui.extend({
        dtree: '{/}__COMMON__/dtree/dtree'
    }).use(['form', 'dtree', 'layer'], function() {
        var form = layui.form;
        var dtree = layui.dtree, layer = layui.layer;

        // 监听提交
        form.on('submit(subForm)', function(data) {

            var checkedNode = dtree.getCheckbarNodesParam("tree");
            var checkedNodeStr = '';
            $.each(checkedNode, function (k, v) {
                checkedNodeStr += v.nodeId + ',';
            });
            data.field.role_node = checkedNodeStr.substr(0, checkedNodeStr.length - 1);

            $.post('{:url("role/add")}', data.field, function (res) {
                if (res.code == 0) {
                    layer.msg(res.msg);

                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.initTable();
                    }, 800);
                } else {
                    layer.msg(res.msg);
                }
            }, 'json');
            return false;
        });

        // 初始化树
        var dtreeObj = dtree.render({
            elem: "#tree",
            data: data,
            checkbar: true,
            dataStyle: 'layStyle',
            initLevel: "0",
            skin: "zdy",
            done: function(res, $ul, first){
                if(first) {
                    dtree.chooseDataInit("tree", "1,2,3");
                }
            }
        });
    });
</script>
</body>
</html>